package com.ebb.develop;

import java.util.HashMap;
import java.util.List;

import android.os.Bundle;
import android.util.Log;
import android.widget.ScrollView;

import com.ebb.R;
import com.ebb.classes.EBBActivity;
import com.ebb.common.EBBConstants;
import com.ebb.ui.EBBTableLayoutForBonList;
import com.parse.FindCallback;
import com.parse.LogInCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParsePush;
import com.parse.ParseQuery;
import com.parse.ParseUser;

public class EBBTestBonListTable extends EBBActivity {
	private static final String TAG = EBBTestBonListTable.class.getCanonicalName();
    private ScrollView scrollView;
    private EBBTableLayoutForBonList listTable;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.table_test);
        this.scrollView = (ScrollView) findViewById(R.id.scroll_view);

        ParseUser.logInInBackground("sjune", "sjune", new LogInCallback() {
            @Override
            public void done(ParseUser parseuser, ParseException e) {
                if(e != null) {
                    e.printStackTrace();
                    return;
                }
                loadBonList();
            }
        });
    }

    private void loadBonList() {
        this.listTable = new EBBTableLayoutForBonList(getApplicationContext());
        
        ParseQuery query = new ParseQuery(EBBConstants.PARSE_CLASS_NAME_BONBON);
        query.include("userObject");
        query.orderByDescending("createdAt");
        query.setLimit(15);
        query.findInBackground(new FindCallback() {
            
            @Override
            public void done(List<ParseObject> list, ParseException e) {
                if(e != null) {
                    e.printStackTrace();
                    return;
                }
                
                HashMap<String, Object> paramHashMap = new HashMap<String, Object>();
                
                Log.d(TAG, "parseObject list.size() >>>" + list.size());
                
                for (ParseObject parseObject : list) {
                    paramHashMap.put("targetObject", parseObject);
                    listTable.addTableRow(paramHashMap);
                    paramHashMap.clear();
                }
                
                // add to scrollview
                scrollView.addView(listTable);
            }
        });
    }
}
